package com.xjrsoft.module.customer.erp_model.customerManage.controller;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.dbmodel.utils.DataSourceUtil;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.sql.SQLException;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customer.erp_model.customerManage.dto.ErpInoutunitDto;
import com.xjrsoft.module.customer.erp_model.customerManage.dto.SaveErpInoutunitFormDataDto;
import com.xjrsoft.module.customer.erp_model.customerManage.vo.ErpInoutunitFormDataVo;
import com.xjrsoft.module.customer.erp_model.customerManage.vo.ErpInoutunitListVo;
import com.xjrsoft.module.customer.erp_model.customerManage.dto.ErpInoutunitListDto;
import com.xjrsoft.module.customer.erp_model.customerManage.vo.ErpInoutunitcontactVo;
import com.xjrsoft.module.customer.erp_model.customerManage.entity.ErpInoutunitcontact;
import com.xjrsoft.module.customer.erp_model.customerManage.dto.ErpInoutunitcontactDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import com.xjrsoft.module.login.utils.SmsKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.customerManage.entity.ErpInoutunit;
import com.xjrsoft.module.customer.erp_model.customerManage.vo.ErpInoutunitVo;
import com.xjrsoft.module.customer.erp_model.customerManage.service.IErpInoutunitService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/customermanage")
@Api(value = "客户管理", tags = "客户管理接口")
public class ErpInoutunitController {


	private final IErpInoutunitService erpInoutunitService;
	/**
	 * 发送短信修改状态
	 */
	@GetMapping("/updateInfo")
	@ApiOperation(value = "发送短信修改状态")
	public Response updateInfo() throws SQLException {
		String sql="select * from table02629 where F_Status is null";
		List<Entity> data= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);
		if(data.size()>0) {
			String info="您有一条测评任务";
//       String info="您有一条测评任务,请进入系统审核,vue.xjrsoft.com:6262";
			for(int i=0;i<data.size();i++)
			{
				String phone=data.get(i).get("send_phone").toString();
				SmsKit.sendMessageByHuaWei(phone,info);
				String Id=data.get(i).get("F_Id").toString();
				String updatesql="update table02629 set F_Status='1' where F_Id='"+Id+"'"+"";
				Db.use(DataSourceUtil.getDataSource("localDB")).execute(updatesql);
			}
		}
		return Response.ok();
	}


	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpInoutunit")
	public Response<ErpInoutunitFormDataVo> getFormData(@PathVariable String id) {
		ErpInoutunitFormDataVo formDataVo = new ErpInoutunitFormDataVo();
		// 主表数据
		ErpInoutunit erpInoutunit = erpInoutunitService.getById(id);
		formDataVo.setErpInoutunitVo(BeanUtil.copy(erpInoutunit, ErpInoutunitVo.class));
		// 从表数据
		formDataVo.setErpInoutunitcontactVoList(BeanUtil.copyList(erpInoutunitService.getErpInoutunitcontactByParentId(id), ErpInoutunitcontactVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpInoutunit")
	public Response<PageOutput<ErpInoutunitListVo>> getPageList(ErpInoutunitListDto listDto) {
		IPage<ErpInoutunit> page = erpInoutunitService.getPageList(listDto);
		List<ErpInoutunitListVo> records = BeanUtil.copyList(page.getRecords(), ErpInoutunitListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpInoutunit")
	public Response save(@RequestBody SaveErpInoutunitFormDataDto formDto) {
		ErpInoutunitDto erpInoutunitDto = formDto.getErpInoutunitDto();
		ErpInoutunit erpInoutunit = BeanUtil.copy(erpInoutunitDto, ErpInoutunit.class);
		List<ErpInoutunitcontactDto> erpInoutunitcontactDtoList = formDto.getErpInoutunitcontactDto();
		List<ErpInoutunitcontact> erpInoutunitcontactList = BeanUtil.copyList(erpInoutunitcontactDtoList, ErpInoutunitcontact.class);
		String dataId = erpInoutunitService.addErpInoutunit(erpInoutunit, erpInoutunitcontactList);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpInoutunit")
	public Response update(@PathVariable String id, @RequestBody SaveErpInoutunitFormDataDto formDto) {
		ErpInoutunitDto erpInoutunitDto = formDto.getErpInoutunitDto();
		ErpInoutunit erpInoutunit = BeanUtil.copy(erpInoutunitDto, ErpInoutunit.class);

		List<ErpInoutunitcontactDto> erpInoutunitcontactDtoList = formDto.getErpInoutunitcontactDto();
		List<ErpInoutunitcontact> erpInoutunitcontactList = BeanUtil.copyList(erpInoutunitcontactDtoList, ErpInoutunitcontact.class);
		return Response.status(erpInoutunitService.updateErpInoutunit(id, erpInoutunit, erpInoutunitcontactList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpInoutunitService.removeByIds(Func.toStrList(ids)));
	}

	
}
